package top.kaoshanji.s1ch08d.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import top.kaoshanji.s1ch08d.model.Product;
import top.kaoshanji.s1ch08d.services.ProductService;

/**
 * 代码清单-s1ch08d-175页a：使用@GetMapping、@PostMapping等具体化注解
 * @author kaoshanji
 * ^_^
 * create time 2025/4/29 18:31
 */
@Controller
public class ProductsController {

    private final ProductService productService;

    public ProductsController(ProductService productService) {
        this.productService = productService;
    }

    @GetMapping("/products")
    public String viewProduct(Model model) {
        model.addAttribute("products", productService.findAll());
        return "products.html";
    }

    @PostMapping("products")
    public String addProduct(Product product, Model model) {
        productService.addProduct(product);

        model.addAttribute("products", productService.findAll());
        return "products.html";
    }

}
